package com.hongxun.modules.bss.controller;


import com.hongxun.common.utils.R;
import com.hongxun.modules.api.entity.BDataDictionaryEntity;
import com.hongxun.modules.api.entity.BMaterialEntity;
import com.hongxun.modules.api.service.BDataDictionaryService;
import com.hongxun.modules.api.service.BMaterialService;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.*;
import com.hongxun.modules.csg.entity.dxf.WordUtil;
import com.hongxun.modules.csg.tools.DxfUtil;
import com.hongxun.modules.csg.tools.StringUtil;
import com.hongxun.modules.sys.controller.AbstractController;
import com.hongxun.modules.sys.entity.SysDeptEntity;
import com.hongxun.modules.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 单线图
 * DXF文件导出Controller
 *
 * Created by john on 2019/8/26.
 */
@RestController
@RequestMapping("/bss/aPBaseDxt")
public class APBaseDXTController extends AbstractController {

    @Value("${com.hongxun.DXFEROTT}")
    private String DXFEROTT;
    @Autowired
    private APBaseService aPBaseService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private DPointService dPointService;
    @Autowired
    private DDrawLineService dDrawLineService;
    @Autowired
    private DCrossCostService dCrossCostService;
    @Autowired
    private DDeviceBuyService dDeviceBuyService;
    @Autowired
    private DMaterialBuyService dMaterialBuyService;
    @Autowired
    private APWireService aPWireService;
    @Autowired
    private APRangeService aPRangeService;
    @Autowired
    private BMaterialService bMaterialService;
    @Autowired
    private BDataDictionaryService bDataDictionaryService;


    /**
     *  导出DXF文件
     *
     * @param request
     * @param response
     * DXF导出需要改到项目储备和施工中去
     */
    @RequestMapping(value = "/expDxfFile")
    public void expDxfFile(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        try {
            String template = "dxf2007_test.ftl";	//模板名称
            String namss=request.getParameter("svgStr").replace("@#@","<")
                    .replace("$#$",">");//对HTML标签进行反转义

            // 生成DXF文件所需组码
            Map<String, Object> result = DxfUtil.svgToDxfData(namss);
            String fileOnlyName = System.currentTimeMillis() + ".dxf";

            String dxf_file_name = request.getParameter("dxf_file_name");//文件名
            if (StringUtil.isNotEmpty(dxf_file_name)) {
                fileOnlyName = dxf_file_name + ".dxf";
            }
            // 存放路径
//            String filePath = "D:\\budgetDXF\\foundation";
            String filePath = DXFEROTT;
            //生成Excel
            String url = WordUtil.createWord(result, template, filePath, fileOnlyName);
            if (StringUtil.isNotEmpty(url)) {
                File file = new File(url);
                //得到文件名
                String filename = file.getName();
                //如果文件不存在会抛出FileNotFoundException异常，需要捕获后进行处理
                InputStream fis = new BufferedInputStream(new FileInputStream(url));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                //清空response
                response.reset();
                String downLoadName = new String(filename.getBytes("gbk"), "iso8859-1");
                // 设置response的Header
                response.addHeader("Content-Disposition", "attachment;filename=" + downLoadName);
                response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } else {
                response.setContentType("text/html");
                try {
                    response.getWriter().print("<html><body><h1>生成dxf文档失败，请刷新后重试！</h1></body></html>");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } catch (Exception e) {
            //log.error(e.getMessage(), e);
            e.printStackTrace();
            response.setContentType("text/html");
            try {
                response.getWriter().print("<html><body><h1>生成dxf文档失败，请刷新后重试！</h1></body></html>");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }


    /**
     * 项目信息组装
     *
     * @param pGuid
     * @return
     * @Time 2018-11-15 16:55:01 修改
     */
    public String getProInfoOnCad(String pGuid){

        StringBuffer sBuff = new StringBuffer();
        List<DPointEntity> list_DPoint_10kv = null;
        List<DPointEntity> list_DPoint_all = null;
        List<APBaseEntity> list_CPBase_10kv = null;
        List<BMaterialEntity> list_BMater = null;

        Map<String , Object> c_10kv_Map = new HashMap<>();
        Map<String , Object> c_all_Map = new HashMap<>();
        DecimalFormat df = new DecimalFormat("######0.000");

        //所有线路
        String c_all = " 1=1 and p_guid='"+pGuid+"' and p_p_datastate != '删除' ";
        //台区
//        if(LineArea != null  && !LineArea.equals("")){
//            c_all += " and p_r_guid='"+LineArea+"'";
//        }
        c_all_Map.put("filterSql",c_all);
        list_DPoint_all = dPointService.queryList(c_all_Map);


		/*  10kv线路导线长度、材料及类型 */
        String c_10kv = " 1=1 and p_guid='"+pGuid+"' and p_p_voltage= '1' and p_p_datastate != '删除' ";
        //台区
//        if(LineArea != null && !LineArea.equals("")){
//            c_10kv += " and p_r_guid='"+LineArea+"'";
//        }
        c_10kv_Map.put("filterSql",c_10kv);
        list_DPoint_10kv = dPointService.queryList(c_10kv_Map);

        String info_10kv = "";
        String where_10kv = " 1=1 ";
        Map<String , Object> map_10kv = new HashMap<>();
        double total_line_length = 0;	//线路总长度
        double line_length_10kv = 0;	//10kv线路长度
        double sum_10kv = 0;

        if (list_DPoint_10kv != null) {
            List<String> list11 = new ArrayList<>();
            for (int i = 0; i < list_DPoint_10kv.size(); i++) {
                // 计算线路长度
                DPointEntity dPointEntity = list_DPoint_10kv.get(i);
                if (dPointEntity.getPPAutospan() == null || dPointEntity.getPPAutospan().toString().equals("0.00")){//档距为空或者为0的情况，
                    line_length_10kv = 0;
                }else{//档距不为空且不为0时
                    double span = Double.parseDouble(dPointEntity.getPPAutospan().toString());
                    total_line_length += span;
                    line_length_10kv += span;
                    sum_10kv += span;
                }
                //选出导线材料ID
                if(dPointEntity.getPWGuid() != null){
                    list11.add(dPointEntity.getPWGuid());
                }

            }
            // 导线材料及类型
            List<String > list_10 = removeDuplicate(list11);
            for (String s : list_10) {
                where_10kv += " and p_w_guid='"+s+"' and p_guid= '"+pGuid+"' and p_p_voltage = '1' and p_p_datastate != '删除' ";
                map_10kv.put("filterSql",where_10kv);
                double wire10 = dPointService.queryWire(map_10kv);
                BMaterialEntity be = bMaterialService.queryObject(s);
                if(wire10!=0 && be != null)
                    info_10kv += be.getMName()+"线型"+df.format(wire10 / 1000)+ "km，";
                where_10kv = "";
            }
        }
        info_10kv = removeDunhao(info_10kv);

		/*  0.38kv线路导线长度、材料及类型 *********/
        List<DPointEntity> list_DPoint_380v = null;
        List<APBaseEntity> list_CPBase_380v = null;
        List<BMaterialEntity> list_BMater_380v = null;
        Map<String , Object> c_380v_Map = new HashMap<>();
        String c_380v = " 1=1 and p_guid='"+pGuid+"' and p_p_voltage= '2' and p_p_datastate != '删除' ";
        //台区
//        if(LineArea != null && !LineArea.equals("")){
//            c_380v += " and p_r_guid='"+LineArea+"'";
//        }
        c_380v_Map.put("filterSql",c_380v);
        list_DPoint_380v = dPointService.queryList(c_380v_Map);

        String info_380v = "";
        String where_380v = "";
        Map<String , Object> map_380v = new HashMap<>();
        double line_length_380v = 0;	//380v线路长度
        double sum_380v = 0;
        if (list_DPoint_380v != null) {
            List<String> list22 = new ArrayList<>();
            for (DPointEntity dPointEntity : list_DPoint_380v) {
                // 计算长度
                if(dPointEntity.getPPAutospan() ==null || dPointEntity.getPPAutospan().toString().equals("0.00")){//档距为空或者为0时
                    line_length_380v = 0;
                }else{//档距不为空且不为0时
                    double span = Double.parseDouble(dPointEntity.getPPAutospan().toString());
                    total_line_length += span;
                    line_length_380v += span;
                    sum_380v += span;
                }
                //导线材料ID
                if(dPointEntity.getPWGuid() != null){
                    list22.add(dPointEntity.getPWGuid());
                }
            }
            // 导线材料及型号
            List<String> list_380 = removeDuplicate(list22);
            for (String s : list_380) {
                where_380v += " 1=1 and p_w_guid='"+s+"' and p_guid= '"+pGuid+"' and p_p_voltage = '2' and p_p_datastate != '删除' ";
                map_380v.put("filterSql",where_380v);
                double wire380 = dPointService.queryWire(map_380v);
                BMaterialEntity be = bMaterialService.queryObject(s);
                if(wire380!=0 && be!= null)
                    info_380v += be.getMName()+"线型"+df.format(wire380 / 1000)+ "km，";
                where_380v = "";
            }

        }
        info_380v = removeDunhao(info_380v);

		/*  0.22kv线路导线长度、材料及类型 *********/
        List<DPointEntity> list_DPoint_220v = null;
        List<APBaseEntity> list_CPBase_220v = null;
        List<BMaterialEntity> list_BMater_220v = null;
        Map<String , Object> c_220v_Map = new HashMap<>();
        String c_220v = " 1=1 and p_guid='"+pGuid+"' and p_p_voltage= '3' and p_p_datastate != '删除' ";
        //台区
//        if(LineArea != null && !LineArea.equals("")){
//            c_220v += " and p_r_guid='"+LineArea+"'";
//        }
        c_220v_Map.put("filterSql",c_220v);
        list_DPoint_220v = dPointService.queryList(c_220v_Map);

        String info_220v = "";
        String where_220v = " 1=1 ";
        Map<String , Object> map_220v = new HashMap<>();
        double line_length_220v = 0;	//10kv线路长度
        double sum_220v = 0;
        if (list_DPoint_220v != null) {
            List<String> list33 = new ArrayList<>();
            for (DPointEntity dPointEntity : list_DPoint_220v) {
                //导线长度
                if(dPointEntity.getPPAutospan() == null || dPointEntity.getPPAutospan().toString().equals("0.00")){//档距为空或者为0时
                    line_length_220v = 0;
                }else{//档距不为空且不为0时
                    double span = Double.parseDouble(dPointEntity.getPPAutospan().toString());
                    total_line_length += span;
                    line_length_220v += span;
                    sum_220v += span;
                }
                //导线材料ID
                if(dPointEntity.getPWGuid() != null){
                    list33.add(dPointEntity.getPWGuid());
                }
            }
            //导线材料及类型
            List<String> list_220 = removeDuplicate(list33);
            for (String s : list_220) {
                where_220v += " and p_w_guid='"+s+"' and p_guid= '"+pGuid+"' and p_p_voltage = '3' and p_p_datastate != '删除' ";
                map_220v.put("filterSql",where_220v);
                double wire220 = dPointService.queryWire(map_220v);
                BMaterialEntity be = bMaterialService.queryObject(s);
                if(wire220!=0 && be != null)
                    info_220v += be.getMName()+"线型"+df.format(wire220 / 1000)+ "km，";
                where_220v = "";
            }
        }
        info_220v = removeDunhao(info_220v);

		/*    信息组装 ******/
        if (total_line_length == 0) {
            sBuff.append("1、本工程线路总长" + df.format(total_line_length / 1000) + "km。");
        } else {
            sBuff.append("1、本工程线路总长" + df.format(total_line_length / 1000) + "km，其中");
            if (info_10kv!=null && !info_10kv.equals("")) {
                sBuff.append("10kV线路" + df.format(sum_10kv / 1000) + "km，" + info_10kv);
                if ((info_380v!=null && !info_380v.equals("")) || (info_220v!=null && !info_220v.equals(""))) {
                    sBuff.append("；");
                }
            }
            if (info_380v!=null && !info_380v.equals("")) {
                sBuff.append("0.4kV线路" + df.format(sum_380v / 1000) + "km，" + info_380v);
                if (info_220v!=null && !info_220v.equals("")) {
                    sBuff.append("；");
                }
            }
            if (info_220v!=null && !info_220v.equals("")) {
                sBuff.append("0.22kV线路" + df.format(sum_220v / 1000) + "km，" + info_220v);
            }
            sBuff.append("。");
        }


        //杆塔
        List<BDataDictionaryEntity> bdlist = new ArrayList<>();
        Map<String ,Object> bdMap = new HashMap<>();
        String str = "";        //  杆塔分高基数
        String oldPole = "";    //  拆旧杆数
        String newPole = "";    //  新装杆数
        int poleTotal = 0;      //  杆塔总基数
        int oldPoleTotal = 0;      //  杆塔利旧杆数
        int newPoleTotal = 0;      //  杆塔新装杆数
        int movePoleTotal = 0;      //  杆塔拆除杆数
        int alld_point = 0;
        String where_pole = " 1=1 ";
        Map<String , Object> map_pole = new HashMap<>();
        List<String> list44 = new ArrayList<>();
        List<DMaterialBuyEntity> dbe = new ArrayList<>();
        List<DMaterialBuyEntity> newdbe = new ArrayList<>();
        List<String> newDmater = new ArrayList<>();
        if (list_DPoint_all != null) {
            for (DPointEntity dPointEntity : list_DPoint_all) {
                if(dPointEntity.getPPName().equals("杆塔")) {
                    // 计算杆塔基数
                    poleTotal += 1;
                    // 获取杆高
                    list44.add(dPointEntity.getPPBar().toString());

                    if(dPointEntity.getPPWorktype().equals("利旧")) {
                        oldPoleTotal += 1;
                    }else if(dPointEntity.getPPWorktype().equals("新增")) {
                        newPoleTotal += 1;
                    }else if(dPointEntity.getPPWorktype().equals("拆除")) {
                        movePoleTotal += 1;
                    }
                }
            }

            //  杆塔梢径分组统计
            map_pole.clear();
            where_pole = "";
            where_pole = " 1=1 and p_guid = '"+pGuid+"' and m_b_bigtype = '杆塔' and m_b_datastate != '删除' GROUP BY m_b_parameter";
            map_pole.put("filterSql",where_pole);
            List<DMaterialBuyEntity> dMaterialBuyEntityList = dMaterialBuyService.queryList(map_pole);
            if(dMaterialBuyEntityList.size()>0){
                for (DMaterialBuyEntity dMaterialBuyEntity : dMaterialBuyEntityList) {
                    map_pole.clear();
                    where_pole = "";
                    where_pole = " 1=1 and m_b_parameter = '"+dMaterialBuyEntity.getMBParameter()+
                            "' and p_guid = '"+pGuid+"' and m_b_bigtype = '杆塔' and m_b_datastate != '删除' ";
                    map_pole.put("filterSql",where_pole);
                    int dmTotal = dMaterialBuyService.queryTotal(map_pole);
                    newPole += dMaterialBuyEntity.getMBParameter()+"型杆塔"+dmTotal+"基；";
                }
            }
//            newPole = removeDunhao(newPole);



            // 获取杆高及基数(去重)
            List<String> list_pole = removeDuplicate(list44);
            map_pole.clear();
            for (String s : list_pole) {
                where_pole = "";
                where_pole = " 1=1 and p_p_datastate != '删除' and p_p_name='杆塔' and p_p_bar='"+s+"' and p_guid='"+pGuid+"'";
                //台区
//                if(LineArea != null && !LineArea.equals("")){
//                    where_pole += " and p_r_guid='"+LineArea+"'";
//                }
                map_pole.put("filterSql",where_pole);
                Integer pole_pbar = dPointService.queryTotal(map_pole);
                s = s.substring(0,s.indexOf("."));
                str = s+"米"+pole_pbar+"基"+"、"+str;
            }
        }
        str = removeDunhao(str);//去除末尾顿号
        if (poleTotal == 0) {
            sBuff.append("2、本工程杆塔" + poleTotal + "基。");
        } else {
//            sBuff.append("2、本工程杆塔" + poleTotal + "基，其中" + str +
//                    ",拆除杆数"+movePoleTotal+
//                    "基，利旧杆数"+oldPoleTotal+
//                    "基，新增杆数"+newPoleTotal+"基。");

            if(newPole != ""){
                sBuff.append("2、本工程杆塔" + poleTotal + "基；新增杆数"+ newPoleTotal+"基，其中"+
                        newPole+"利旧杆数"+ oldPoleTotal+"基；拆除杆数"+movePoleTotal+"基。");
            }else{
                sBuff.append("2、本工程杆塔" + poleTotal + "基；新增杆数"+ newPoleTotal+"基；"+
                        newPole+"利旧杆数"+ oldPoleTotal+"基；拆除杆数"+movePoleTotal+"基。");
            }

        }


        //变压器
        List<BDataDictionaryEntity> bylist = new ArrayList<>();
        Map<String ,Object> byMap = new HashMap<>();
        String str22 = "";
        String transInfo = "";  //  变压器描述信息
        int transTotal = 0;
        int alld_point1 = 0;    //  变压器总数
        String where_T = " 1=1 ";
        Map<String , Object> map_T = new HashMap<>();
        List<String> list55 = new ArrayList<>();
        boolean tr = false ;
        int areByq = 0;
        if (list_DPoint_all != null) {
            for (DPointEntity dPointEntity : list_DPoint_all) {
                if(dPointEntity.getPPName().equals("柱上变压器")) {
                    alld_point1 +=1;
                    list55.add(dPointEntity.getGuid());
//                    tr = true;
                }
            }
//            transInfo += "变压器"+alld_point1+"台";
            if(list55.size()>0){
                for (String s : list55) {
                    where_T = " and p_p_guid='" + s + "' and d_b_bigtype='变压器' and d_b_datastate !='删除' ";
                    map_T.put("filterSql", where_T);
                    List<DDeviceBuyEntity> DDeviceList = dDeviceBuyService.queryList(map_T);
                    int dbs = dDeviceBuyService.queryTotal(map_T);
                    if(DDeviceList.size()>0){
                        tr = true;
                        for (DDeviceBuyEntity dDeviceBuyEntity : DDeviceList) {
                            str22 += dDeviceBuyEntity.getDBParameter();
                        }
                    }
                    if(tr)
                        transInfo += str22+"型变压器"+dbs+"台；";
                }
            }
            transInfo = removeDunhao(transInfo);
            transInfo += "。";

            if(tr == false)sBuff.append("3、变压器"+alld_point1+"台。");
            else sBuff.append("3、变压器"+alld_point1+"台；其中"+transInfo);
        }else {
            sBuff.append("3、变压器0台。");
        }


        //交叉跨越
        List<DCrossCostEntity> list_dcross = new ArrayList<>();
        List<String> list_dcross_new = new ArrayList<>();
        Map<String , Object> crossMap = new HashMap<>();
        String crossWhere = " 1=1 and p_guid='"+pGuid+"' and c_c_datastate != '删除' ";
        //台区
//        if(LineArea != null && !LineArea.equals("")){
//            crossWhere += " and p_r_guid='"+LineArea+"'";
//        }
        crossMap.put("filterSql",crossWhere);
        list_dcross = dCrossCostService.queryList(crossMap);
        String cross_info = "";
        if (list_dcross.size()>0) {
            sBuff.append("4、交叉跨越：");
            crossMap.clear();
            int aa = 0 ;
            for (DCrossCostEntity listDcross : list_dcross) {
                list_dcross_new.add(listDcross.getCType());
            }
            list_dcross_new = removeDuplicate(list_dcross_new);
            for (String s : list_dcross_new) {
                String cross_1 = " 1=1 and p_guid='"+pGuid+"' and c_type = '"+s+"' and c_c_datastate != '删除' ";
                //台区
//                if(LineArea != null && !LineArea.equals("")){
//                    cross_1 += " and p_r_guid='"+LineArea+"'";
//                }
                crossMap.put("filterSql",cross_1);
                aa = dCrossCostService.queryTotal(crossMap);    //  分属跨越物个数
                crossMap.clear();
                cross_1 ="";
                cross_1+=" 1=1 and  d_code = 'cross_type' ";
                crossMap.put("filterSql",cross_1);
                List<BDataDictionaryEntity> bDataDictionaryList = bDataDictionaryService.queryList(crossMap);
                for (BDataDictionaryEntity bDataDictionaryEntity : bDataDictionaryList) {
                    if(s.equals(bDataDictionaryEntity.getDKey())){
                        cross_info +=  bDataDictionaryEntity.getDValue()+aa+"处、";
                    }
                }
            }
            cross_info  = list_dcross.size() + "处"+"，其中"+cross_info;
            cross_info = removeDunhao(cross_info);
            sBuff.append(cross_info+"。");
        }else {
            sBuff.append("4、交叉跨越0处。");
        }

        return sBuff.toString();
    }

    /**
     *  单线路作图点位上下联关系集合
     *
     * @param pointList
     * @return
     */
    private List<DPointEntity> pointInfo(List<DPointEntity> pointList){

        Map<String , Object> pointMap = new HashMap<>();
        List<DPointEntity> newPoints = new ArrayList<>();       //  点位数据
        List<DPointEntity> temporaryPoints = new ArrayList<>(); //  临时数据

        for (DPointEntity dPointEntity : pointList) {
                //  首先查询出第一个点位，查询出第一个点后用该点查询下联点位
            if(dPointEntity.getPPUpguid()==null || dPointEntity.getPPUpguid().equals("")){
                temporaryPoints.add(dPointEntity);
            }
        }

        for (int j = 0; j < temporaryPoints.size(); j++) {
            String rangeWhere = " 1=1 and p_p_upguid='"+ temporaryPoints.get(j).getGuid()+"' and p_p_datastate != '删除' ";
            pointMap.put("filterSql", rangeWhere);
            newPoints = dPointService.queryList(pointMap);
            if(newPoints.size()>0){
                for (DPointEntity newPoint : newPoints) {
                    temporaryPoints.add(newPoint);
                }
            }
        }

        return temporaryPoints;
    }


    /**
     * 获取所有点位信息
     * @param pGuid		项目ID
     * @param
     * @return
     * @addTime 2018-10-17 11:31:54
     */
    @RequestMapping("getAllPointInfo")
    public R getAllPointInfo(String pGuid){

        // 点位表中的p_r_guid和交叉跨越表中的p_r_guid都是所属项目范围ID（线路台区ID），和项目范围表a_p_range进行关联


        //组装数据
        List<APRangeEntity> apRangeList = new ArrayList();
        List<DPointEntity> pointList = new ArrayList<>();
        List<DPointEntity> pointListForNode = new ArrayList<>();
        List<DPointEntity> dPointEntityList = new ArrayList<>();
        List<DDrawLineEntity> laxList = new ArrayList<>();
        List<DDrawLineEntity> laxianList = new ArrayList<>();
        List<DCrossCostEntity> dCrossList = new ArrayList<>();
        List<DCrossCostEntity> dCrossCostList = new ArrayList<>();
        Map<String , Object> pointMap = new HashMap<>();
//		String pGuid = params.get("pGuid").toString();//项目ID

        //	单线图作图时，只统计单个台区的情况，多个台区时不统计
        String rangeWhere = " 1=1 and p_guid='"+pGuid+"' and p_r_datastate != '删除' ";
        pointMap.put("filterSql", rangeWhere);
        apRangeList = aPRangeService.queryList(pointMap);
        if(apRangeList!=null && apRangeList.size() ==1){
            //1.根据项目ID获取点位信息
            pointMap.clear();
            DPointEntity dPointEntitys = new DPointEntity();
            dPointEntitys.setPGuid(pGuid);
            String pointWhere = " 1=1 and p_guid='"+pGuid+"' and p_p_worktype='新增' and p_p_datastate != '删除' ";
            pointMap.put("filterSql", pointWhere);
            pointList = dPointService.queryDxtList(dPointEntitys);

            //2.点位作图时需要涉及到的信息
            if(pointList.size()>0){

                dPointEntityList = pointInfo(pointList);

                for (DPointEntity dPointEntity : dPointEntityList) {

                    //  当前点位作为上联时，有多少个下联点位
                    pointMap.clear();
                    String nextWhere = " 1=1 and p_p_upguid= '"+dPointEntity.getGuid()+"' and p_p_worktype='新增' and p_p_datastate != '删除'";
                    pointMap.put("filterSql", nextWhere);

                    int nextNum = dPointService.queryTotal(pointMap);  //  点位的下联分支数

                    //  是否节点
                    if(nextNum==0 || nextNum==1) {
                        dPointEntity.setIsNode(false);
                        dPointEntity.setNextNum(0);
                    }else {
                        dPointEntity.setIsNode(true);
                        dPointEntity.setNextNum(nextNum);
                        pointListForNode = dPointService.queryList(pointMap);   //  所含关联下联点位信息
                        for (DPointEntity pointEntity : pointListForNode) {
                            pointEntity.setIsNode(true);
                        }
                    }




                    //  对杆塔梢径进行设置
                    if(dPointEntity.getPPName().equals("杆塔")){
                        pointMap.clear();
                        String materWhere = " 1=1 and p_guid = '"+pGuid+"' and p_p_guid = '"+
                                dPointEntity.getGuid()+"' and m_b_bigtype = '杆塔' and m_b_datastate != '删除' ";
                        pointMap.put("filterSql", materWhere);
                        List<DMaterialBuyEntity> mb = dMaterialBuyService.queryList(pointMap);
                        if(mb.size()>0){
                            for (DMaterialBuyEntity dMaterialBuyEntity : mb) {
                                String par = dMaterialBuyEntity.getMBParameter();
                                String parameter = "";
                                String pse = "";
                                Integer dl = 0 ;
                                //  匹配梢径
                                if(par.indexOf("Φ") != -1 && par.indexOf("×") != -1){   //  点位材料表中有Φ标记时
                                    if(par.indexOf("Φ") == 0){
                                        parameter += par.substring(par.indexOf("Φ")+1,par.indexOf("×"));
                                        dl = Integer.parseInt(parameter);
                                        if(dl > 190) pse = dl.toString();
                                    }
                                }else if(par.indexOf("，") != -1 && par.indexOf("m") != -1){ //  点位材料表中没有Φ标记时
                                    parameter += par.substring(par.indexOf("，")+1,par.indexOf("m"));
                                    dl = Integer.parseInt(parameter);
                                    if(dl > 190) pse = dl.toString();
                                }
                                if(pse != "" )dPointEntity.setPPType("("+pse+")");
                                else dPointEntity.setPPType("");
                            }
                        }
                    }



                    //杆塔拉线
                    pointMap.clear();
                    if(dPointEntity.getPPName().equals("杆塔") && dPointEntity.getPPWirenum()>=1) {
                        String laxianWhere = " 1=1 and p_guid='" + pGuid + "' and p_p_guid='" + dPointEntity.getGuid() + "' and d_l_datastate !='删除' ";
                        pointMap.put("filterSql", laxianWhere);
                        laxList = dDrawLineService.queryList(pointMap);
                        laxianList.addAll(laxList);
                    }

                    //交叉跨越
                    pointMap.clear();
                    String crossCostWhere = " 1=1 and c_c_upguid='"+dPointEntity.getGuid()+"' and c_c_datastate !='删除'";

                    pointMap.put("filterSql", crossCostWhere);
                    dCrossList = dCrossCostService.queryList(pointMap);
                    dCrossCostList.addAll(dCrossList);

                }
            }

            //信息描述
            String pointInfo = getProInfoOnCad(pGuid);

            //项目名称
            APBaseEntity apBaseEntity = aPBaseService.queryObject(pGuid);
            String projectName = apBaseEntity.getPBName();

            //设计单位
            SysDeptEntity sysDeptEntity = sysDeptService.queryObject(apBaseEntity.getPBOrgid());
            String complanName = sysDeptEntity.getName();

            // 模板数据传递
            return R.ok().put("pointList",dPointEntityList)
                    .put("pointListForNode",pointListForNode)
                    .put("laxianList",laxianList)
                    .put("crossCostList",dCrossCostList)
                    .put("textgcmc",projectName)
                    .put("textsjdw",complanName)
                    .put("projectName",projectName)
                    .put("pointInfo",pointInfo);
        }

        return R.ok();
    }



    /**
     *  list 去重
     *
     * @param list
     * @return
     */
    private static List<String> removeDuplicate(List<String> list) {
        LinkedHashSet<String> set = new LinkedHashSet<String>(list.size());
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    /**
     * 去掉末尾顿号
     * @param str
     * @return
     */
    public String removeDunhao(String str){
        if(str!=null && !str.equals("")){
            if( ( str.lastIndexOf("、") == str.length()-1 ) ||
                    ( str.lastIndexOf("，") == str.length()-1 ) ){
                str = str.substring(0,str.length()-1);
            }
        }
        return str;
    }

}
